/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: RegionServiceIMPL
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/30 18:59
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketRegion;
import com.cskaoyan.bean.MarketRegionExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
* 行政区域的信息回显
* */
@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    MarketRegionMapper marketRegionMapper;
    @Override
    public CommonData<MarketRegion> getAllRegionInfo() {
        //先查询出所有的国家   pid为0的即为国家
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        //设置查询条件
        criteria.andPidEqualTo(0);
        //查询出所有的省
        List<MarketRegion> provinceList = marketRegionMapper.selectByExample(marketRegionExample);
        //根据省的id查询出下面的市
        for (MarketRegion province : provinceList) {
            //拿到省的id
            Integer provinceId = province.getId();
            marketRegionExample.clear();
            MarketRegionExample.Criteria urbanCriteria = marketRegionExample.createCriteria();
            //根据省的id作为自己的pid查询
            urbanCriteria.andPidEqualTo(provinceId);
            //查询出市区表
            List<MarketRegion> UrbanList=marketRegionMapper.selectByExample(marketRegionExample);
            //将市区的id作为镇的pid进行查询下面的镇
            for (MarketRegion Urban : UrbanList) {
                //拿到市区的id
                Integer urbanId = Urban.getId();
                marketRegionExample.clear();
                MarketRegionExample.Criteria countyCriteria=marketRegionExample.createCriteria();
                countyCriteria.andPidEqualTo(urbanId);
                //拿到当前市区下面的镇的所有集合
                List<MarketRegion> countyList=marketRegionMapper.selectByExample(marketRegionExample);
                //将这个集合赋值给当前市区
                Urban.setChildren(countyList);
            }
            //将得到的市区给当前的省赋值
            province.setChildren(UrbanList);
        }
        System.out.println(provinceList);
        //获取页码信息
        PageInfo<MarketRegion> marketRegionPageInfo = new PageInfo<>(provinceList);
        int pageNum = marketRegionPageInfo.getPageNum();
        int pageSize = marketRegionPageInfo.getPageSize();
        int pages = marketRegionPageInfo.getPages();
        //讲将上面的值赋值给需要返回的commonData
        CommonData<MarketRegion> marketRegionCommonData = new CommonData<>();
        marketRegionCommonData.setPages(pages);
        marketRegionCommonData.setTotal(pageSize);
        marketRegionCommonData.setPage(pageNum);
        marketRegionCommonData.setLimit(pageSize);
        marketRegionCommonData.setList(provinceList);
        return marketRegionCommonData;
    }
}
